package sql;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import javax.jdo.Extent;
import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Query;
import javax.jdo.Transaction;

import Server.Song;



public class SongDAO implements ISongDAO {

	private PersistenceManagerFactory pmf;
	
	public SongDAO(){
		// Create a PersistenceManagerFactory for this datastore
		pmf = JDOHelper.getPersistenceManagerFactory("datanucleus.properties");
        System.out.println("DataNucleus AccessPlatform with JDO. PMFactory created");
	}

	@Override
	public void storeSong(Song p) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Collection<Song> getSongs() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void updateSongs(Song p) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void deleteSongs() {
		// TODO Auto-generated method stub
		
	}
	
      
	
/*
	@Override
	public void storeBook(Book p) {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		PersistenceManager pm = pmf.getPersistenceManager();
	    Transaction tx=pm.currentTransaction();
	   
	    try {
	       tx.begin();
	       System.out.println("Persisting BOOK: " + p.getName());
	       pm.makePersistent(p);
	       tx.commit();

	     }
	     finally {
	       if (tx.isActive()) {
	                tx.rollback();
	       }
	           pm.close();
	       }
	       System.out.println("");
	    
	}

	@Override
	public Collection<Product> getProducts() {
		// TODO Auto-generated method stub
		PersistenceManager pm = pmf.getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		Collection<Product> products = new ArrayList<Product>();
	      try
	        {
	            tx.begin();
	            System.out.println("Retrieving Extent for Products");
	            Extent<Product> e = pm.getExtent(Product.class, true);
	            Iterator<Product> iter = e.iterator();
	            while (iter.hasNext())
	            {
	                Object obj = iter.next();
	                products.add((Product) obj);
	            }
	            pm.getFetchPlan().addGroup("detach_book_copies");
		        pm.getFetchPlan().setMaxFetchDepth(2);
	            tx.commit();
	        }
	        catch (Exception e)
	        {
	            System.out.println("Exception thrown during retrieval of Extent : " + e.getMessage());
	        }
	        finally
	        {
	            if (tx.isActive())
	            {
	                tx.rollback();
	            }
	            pm.close();
	        }
	        System.out.println("");
		return products;
	}

	@Override
	public Collection<Product> queryProducts(String condition) {
		// TODO Auto-generated method stub
		PersistenceManager pm = pmf.getPersistenceManager();
	    Transaction tx = pm.currentTransaction();
	    Collection<Product> c = null;
	        try
	        {
	            tx.begin();
	            System.out.println("Executing Query for Products given a condition");
	            Extent<Product> e=pm.getExtent(Product.class,true);
	            Query q=pm.newQuery(e, condition);
	            c=(Collection<Product>)q.execute();
	            Iterator iter=c.iterator();
	            while (iter.hasNext())
	            {
	                Object obj = iter.next();
	                System.out.println(">  " + obj);
           
	            }
	            
	            tx.commit();
	        }
	        finally
	        {
	            if (tx.isActive())
	            {
	                tx.rollback();
	            }
	            pm.close();
	        }
	        System.out.println("");
		return c;
	}

	@Override
	public void updateProduct(Product p) {
		// TODO Auto-generated method stub
		
		PersistenceManager pm = pmf.getPersistenceManager();
	    Transaction tx=pm.currentTransaction();
	    try {
	    	tx.begin();
	    	pm.makePersistent(p);
	    	tx.commit();
	    	System.out.println("Product has been updated..." + p.getName());
	     }
	     finally {
	       if (tx.isActive()) {
	                tx.rollback();
	       }
	           pm.close();
	       }
	       System.out.println("");
	}
	public Product findProduct(String identifier){
		PersistenceManager pm = pmf.getPersistenceManager();
		Transaction tx=pm.currentTransaction();
		Product p;
	    try {
	    	tx.begin();
	    	System.out.println ("Querying Product: " + identifier);
	    	Query q = pm.newQuery("SELECT FROM " + Product.class.getName() + " WHERE name == \"" + identifier + "\"");
	    	q.setUnique(true);
	    	p = (Product)q.execute();
	    	pm.getFetchPlan().addGroup("detach_book_copies");
	        pm.getFetchPlan().setMaxFetchDepth(2);
	    	tx.commit();
	     }
	     finally {
	       if (tx.isActive()) {
	                tx.rollback();
	       }
	           pm.close();
	       }
	       System.out.println("");
	       return p;	
	}
		

	@Override
	public void deleteProducts() {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void storeSong(Song p) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public Collection<Song> getSongs() {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public void updateSongs(Song p) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void deleteSongs() {
		// TODO Auto-generated method stub
		
	}
	*/

}
